package com.useeinfo.asg.modules.security.entity;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.entity.Address;
import com.useeinfo.asg.modules.logging.entity.Resource;
import com.useeinfo.framework.extend.entity.DataEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import net.sf.json.JSONObject;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**

 */
@Entity
@Table(name = "system_user")
public class SystemUser extends DataEntity<SystemUser> {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;					// 主键编号

	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;			// 创建记录时间

	@Column(columnDefinition = "bit default 0")
	private boolean deleteStatus;		// 逻辑删除状态    0.否    1.是

	@Column(columnDefinition = "tinyint default 0")
	private int departmentMark;		//  所属部门    1.发改委  2.统计局  3.全部

	/**
	 * 系统
	 */

	private int type; //0.ios版本  1.Android版本

	/**
	 * 版本号
	 * */
	private String versionNum;
	/**
	 * 用户登陆帐号
	 */
	private String userAccount;

	/**
	 * 用户手机号
	 */
	private String mobile;

	/**
	 * 用户邮箱
	 */
	private String email;

	/**
	 * 用户登陆密码
	 */
	private String userPassword;

	/**
	 * 用户姓名
	 */
	private String userName;

	/**
	 * 职务
	 */
	private String postion;

	/**
	 * 是否平台管理员 true-平台管理员 false-小区管理员
	 */
	private boolean systemManager;

	@ManyToOne
	@JoinColumn(name = "address_id")
	private Address address;

	/**
	 * 头像
	 */
	@ManyToOne
	@JoinColumn(name = "resource_id")
	private Resource resource;


	/**
	 * 用户角色
	 */
	@JoinTable(name = "user_role",joinColumns = @JoinColumn(name = "user_id", referencedColumnName="ID"),inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName="ID"))
	@ManyToMany(fetch = FetchType.EAGER)
	private Set<SystemRole> systemRoleSet = new HashSet();

	/**
	 * 是否被禁用 true-正常 false-已禁用
	 */
	private boolean enabled;

	/**
	 * 假删除 true-正常 false-已删除
	 */
	private boolean valid = true;

	/**
	 * 设备别名
	 */
	private String alias;

	public SystemUser() {
	}

	public SystemUser(String userAccount, String userPassword, String userName) {
		this.userAccount = userAccount;
		this.userPassword = userPassword;
		this.userName = userName;
	}

	@Override
	public JSONObject toJSONObject() {
		JSONObject jsonObject = new JSONObject();

		jsonObject.put("userId", getId());
		jsonObject.put("createTime", CommUtil.formatLongDate(getCreateTime()));
		jsonObject.put("userAccount", CommonSugar.getTypedDefault(getUserAccount(), ""));
		jsonObject.put("userPassword", CommonSugar.getTypedDefault(getUserPassword(), ""));
		jsonObject.put("userName", CommUtil.null2String(this.getUserName()));
		jsonObject.put("mobile", CommUtil.null2String(this.getMobile()));
		jsonObject.put("email", CommUtil.null2String(this.getEmail()));
		jsonObject.put("postion", CommUtil.null2String(this.getPostion()));
		jsonObject.put("company", CommUtil.null2String(this.getAddress()==null?"":this.getAddress().getAddrName()));
		jsonObject.put("resource",CommUtil.null2String(this.getResource()==null?"":this.getResource().getResPath()));
		return jsonObject;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getVersionNum() {
		return versionNum;
	}

	public void setVersionNum(String versionNum) {
		this.versionNum = versionNum;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public boolean isSystemManager() {
		return systemManager;
	}

	public void setSystemManager(boolean systemManager) {
		this.systemManager = systemManager;
	}

	public Set<SystemRole> getSystemRoleSet() {
		return systemRoleSet;
	}

	public void setSystemRoleSet(Set<SystemRole> systemRoleSet) {
		this.systemRoleSet = systemRoleSet;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	@Override
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public boolean isDeleteStatus() {
		return deleteStatus;
	}

	public void setDeleteStatus(boolean deleteStatus) {
		this.deleteStatus = deleteStatus;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public String getPostion() {
		return postion;
	}

	public void setPostion(String postion) {
		this.postion = postion;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public int getDepartmentMark() {
		return departmentMark;
	}

	public void setDepartmentMark(int departmentMark) {
		this.departmentMark = departmentMark;
	}

	public String getDepartmentDesc() {

		if(this.getDepartmentMark() == 1) {
			return "发改委";
		}else if(this.getDepartmentMark() == 2) {
			return "统计局";
		}else if(this.getDepartmentMark() == 3) {
			return "全部门";
		}
		return "无部门";
	}

	@Override
	public boolean equals(Object obj) {

		System.out.println("------equals----");

		if (obj instanceof SystemUser) {

			SystemUser user = (SystemUser) obj;

			if (this.getId().equals(user.getId())) {
				return true;
			}

		}
		return false;

	}
}
